#####################################################
# Replication Archive: Crumbling in the Face of Cost?
# Mader, Neubert, Münchow, Hofmann, Schoen & Gavras
# 01/2024
#####################################################
# Appendix 
#####################################################

# define which packages are needed 
p_needed <-
  c("broom", "hrbrthemes", "viridis", "ggh4x", "tidyverse",
    "sjPlot", "dotwhisker", "gridExtra", "grid", "extrafont",
    "ggrepel", "hrbrthemes", "ggExtra")

# check which packages are already installed
packages <- rownames(installed.packages())

# check which of the packages you need are not installed 
p_to_install <- p_needed[!(p_needed %in% packages)]

# install missing packages 
if (length(p_to_install) > 0) {
  install.packages(p_to_install)
}
# load required packages 
sapply(p_needed, require, character.only = TRUE)

###################################################

# load data
load("EUP_Costs_ReplicationData.RData")

###################################################
# S2: Question order effects
### Question order of Experiments

# Experiment 1, Question order effects
question_order_ex1 <- dat %>%
  mutate(costExperiment1_Treat1 = fct_recode(costExperiment1_Treat1, `Casualties` = "SoldiersRisk"),
         costExperiment1_Treat2 = fct_recode(costExperiment1_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost"),
         Q31_32_order = fct_recode(Q31_32_order, `Military Intervention\n First` = "1", `Defence Procurement\n First` = "2")) %>%
  lm(costExperiment1_CrisisManagement ~  costExperiment1_Treat1 * Q31_32_order + costExperiment1_Treat2 * Q31_32_order + QCOUNTRY, data = .)

# Experiment 2, Question order effects
question_order_ex2 <- dat %>%
  mutate(costExperiment2_Treat1 = fct_recode(costExperiment2_Treat1, `Job losses` = "JobCuts"),
         costExperiment2_Treat2 = fct_recode(costExperiment2_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost"),
         Q31_32_order = fct_recode(Q31_32_order, `Military Intervention\n First` = "1", `Defence Procurement\n First` = "2")) %>%
  lm(costExperiment2_Aircraft ~ costExperiment2_Treat1 * Q31_32_order + costExperiment2_Treat2 * Q31_32_order + QCOUNTRY, data = .)

# Plot: Experiment 1, Non-Budgetary Costs
question_order_Ex1_1 <- plot_model(question_order_ex1, type = "pred", terms = c("Q31_32_order", "costExperiment1_Treat1"), colors = c("#87a96b", "#800080")) +
  labs(y = "Predicted Value", x = "Question Order", title = "Support for Military Intervention", color = "Non-Budgetary Costs") +
  lims(y = c(0.1,0.6)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.position = "bottom",
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 1.5, 0.5, 0.5, "cm")) #+ coord_fixed(ratio=1)

# Plot: Experiment 1, Budgetary Costs
question_order_Ex1_2 <- plot_model(question_order_ex1, type = "pred", terms = c("Q31_32_order", "costExperiment1_Treat2"), colors = c("#87a96b", "#ffb653", "#6b87a9")) +
  labs(y = "Predicted Value", x = "Question Order", title = "Support for Military Intervention", color = "Budgetary Costs") +
  lims(y = c(0.1,0.6)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.position = "bottom",
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 1.5, 0.5, 0.5, "cm")) #+ coord_fixed(ratio=1)

# Plot: Experiment 2, Non-Budgetary Costs
question_order_Ex2_1 <- plot_model(question_order_ex2, type = "pred", terms = c("Q31_32_order", "costExperiment2_Treat1"), colors = c("#87a96b", "#800080")) +
  labs(y = "Predicted Value", x = "Question Order", title = "Support for Defence Procurement", color = "Non-Budgetary Costs") +
  lims(y = c(0.1,0.6)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.position = "bottom",
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 1.5, 0.5, 0.5, "cm")) #+ coord_fixed(ratio=1)

# Plot: Experiment 2, Budgetary Costs
question_order_Ex2_2 <- plot_model(question_order_ex2, type = "pred", terms = c("Q31_32_order", "costExperiment2_Treat2"), colors = c("#87a96b", "#ffb653", "#6b87a9")) +
  labs(y = "Predicted Value", x = "Question Order", title = "Support for Defence Procurement", color = "Budgetary Costs") +
  lims(y = c(0.1,0.6)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.position = "bottom",
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 1.5, 0.5, 0.5, "cm")) #+ coord_fixed(ratio=1)

# add whitespace
whitespace <- grob("")

# arrange plots
question_order <- grid.arrange(question_order_Ex1_1, question_order_Ex2_1,
                               question_order_Ex1_2, question_order_Ex2_2, ncol = 2)

ggsave("het_question_order.png", question_order, device = "jpeg", width = 4600 , height = 3000, units = "px")


# S3: Expanded Models

# Experiment 1: Pooled Results with controls
experiment1_Aggregate_c <- dat %>%
  mutate(costExperiment1_Treat1 = fct_recode(costExperiment1_Treat1, socialCosts = "SoldiersRisk")) %>%
  dplyr::select(costExperiment1_CrisisManagement, costExperiment1_Treat1, costExperiment1_Treat2, QCOUNTRY, female, education, age, attitudes_EU, attitudes_NATO, attachmentCountry, attachmentEurope, compromise, militarism, threat, quiz, governmentVote, csdp_wouldlike, leftright) %>%
  dplyr::rename(costExperiment_Treat1 = costExperiment1_Treat1, costExperiment_Treat2 = costExperiment1_Treat2) %>%
  do(broom::tidy(lm(costExperiment1_CrisisManagement ~  costExperiment_Treat1 + costExperiment_Treat2 + attitudes_EU + attachmentCountry + attachmentEurope + compromise + militarism + threat + quiz + governmentVote + QCOUNTRY, data = .))) %>%
  dplyr::mutate(dv = "Military Intervention",
                model = "With Controls") %>%
  dplyr::filter(str_detect(term, "costEx"))

# Experiment 1: Pooled without controls
experiment1_Aggregate_p <- dat %>%
  mutate(costExperiment1_Treat1 = fct_recode(costExperiment1_Treat1, socialCosts = "SoldiersRisk")) %>%
  dplyr::select(costExperiment1_CrisisManagement, costExperiment1_Treat1, costExperiment1_Treat2, QCOUNTRY, female, education, age, attitudes_EU, attitudes_NATO, attachmentCountry, attachmentEurope, compromise, militarism, threat, quiz, governmentVote, csdp_wouldlike, leftright) %>%
  dplyr::rename(costExperiment_Treat1 = costExperiment1_Treat1, costExperiment_Treat2 = costExperiment1_Treat2) %>%
  do(broom::tidy(lm(costExperiment1_CrisisManagement ~  costExperiment_Treat1 + costExperiment_Treat2 + QCOUNTRY, data = .))) %>%
  dplyr::mutate(dv = "Military Intervention",
                model = "Without Controls") %>%
  dplyr::filter(str_detect(term, "costEx"))

# Experiment 2: Pooled with controls
experiment2_Aggregate_c <- dat %>%
  mutate(costExperiment2_Treat1 = fct_recode(costExperiment2_Treat1, socialCosts = "JobCuts")) %>%
  dplyr::select(costExperiment2_Aircraft, costExperiment2_Treat1, costExperiment2_Treat2, QCOUNTRY, female, education, age, attitudes_EU, attitudes_NATO, attachmentCountry, attachmentEurope, compromise, militarism, threat, quiz, governmentVote, csdp_wouldlike, leftright) %>%
  dplyr::rename(costExperiment_Treat1 = costExperiment2_Treat1, costExperiment_Treat2 = costExperiment2_Treat2) %>%
  do(broom::tidy(lm(costExperiment2_Aircraft ~  costExperiment_Treat1 + costExperiment_Treat2 + attitudes_EU + attachmentCountry + attachmentEurope + compromise + militarism + threat + quiz + governmentVote + QCOUNTRY, data = .))) %>%
  mutate(dv = "Defence Procurement",
         model = "With Controls") %>%
  dplyr::filter(str_detect(term, "costEx"))

# Experiment 2: Pooled without controls
experiment2_Aggregate_p <- dat %>%
  mutate(costExperiment2_Treat1 = fct_recode(costExperiment2_Treat1, socialCosts = "JobCuts")) %>%
  dplyr::select(costExperiment2_Aircraft, costExperiment2_Treat1, costExperiment2_Treat2, QCOUNTRY, female, education, age, attitudes_EU, attitudes_NATO, attachmentCountry, attachmentEurope, compromise, militarism, threat, quiz, governmentVote, csdp_wouldlike, leftright) %>%
  dplyr::rename(costExperiment_Treat1 = costExperiment2_Treat1, costExperiment_Treat2 = costExperiment2_Treat2) %>%
  do(broom::tidy(lm(costExperiment2_Aircraft ~  costExperiment_Treat1 + costExperiment_Treat2 + QCOUNTRY, data = .))) %>%
  mutate(dv = "Defence Procurement",
         model = "Without Controls") %>%
  dplyr::filter(str_detect(term, "costEx"))

### bind results from models with and without control variables for comparison
both_experiments <- rbind(experiment1_Aggregate_p,
                          experiment1_Aggregate_c,
                          experiment2_Aggregate_p,
                          experiment2_Aggregate_c)

# name results for visualization
both_experiments$term[1] <- "Non-Budgetary \nCosts: Casualties"
both_experiments$term[2] <- "Budgetary Costs: \nNational Budget"
both_experiments$term[3] <- "Budgetary Costs: \nPooled Budget"
both_experiments$term[4] <- "Non-Budgetary \nCosts: Casualties"
both_experiments$term[5] <- "Budgetary Costs: \nNational Budget"
both_experiments$term[6] <- "Budgetary Costs: \nPooled Budget"
both_experiments$term[7] <- "Non-Budgetary \nCosts: Job Cuts"
both_experiments$term[8] <- "Budgetary Costs: \nNational Budget"
both_experiments$term[9] <- "Budgetary Costs: \nPooled Budget"
both_experiments$term[10] <- "Non-Budgetary \nCosts: Job Cuts"
both_experiments$term[11] <- "Budgetary Costs: \nNational Budget"
both_experiments$term[12] <- "Budgetary Costs: \nPooled Budget"

# transform to factor and relevel
both_experiments$term <- as.factor(both_experiments$term)
both_experiments$term <- forcats::fct_relevel(both_experiments$term,
                                              "Non-Budgetary \nCosts: Casualties",
                                              "Non-Budgetary \nCosts: Job Cuts",
                                              "Budgetary Costs: \nNational Budget",
                                              "Budgetary Costs: \nPooled Budget")
# reverse factors
both_experiments <- both_experiments %>%
  mutate(term = forcats::fct_rev(term))

# plot results from models with and without controls
plotExperiment_Aggregate_c <- ggplot(both_experiments,
                                     aes(estimate, fct_rev(term), color = model
                                     )) +
  geom_point(size = 3.5, position = position_dodge(.3)) +
  geom_linerange(aes(xmin = estimate -1.96*std.error,
                     xmax = estimate + 1.96*std.error),
                 size = 1.5,
                 position = position_dodge(.3),
                 alpha = 0.7) +
  theme_ipsum_rc(grid="Y",
                 axis_title_size = 18,
                 axis_text_size = 18,
                 strip_text_size = 24,
                 strip_text_face = "bold") +
  geom_vline(xintercept = 0, lty = 2, color = "darkgrey", size = 0.75) +
  labs(y = "", x = "") +
  facet_grid(~(factor(dv, levels = c("Military Intervention", "Defence Procurement"))), scales = "free") +
  theme(legend.position = "bottom",
        legend.title= element_blank()) +
  lims(x = c(-0.15,0.06)) +
  #scale_y_discrete(labels= labels) +
  coord_flip() +
  scale_color_manual(values = c("black", "darkgrey")) +
  theme(text = element_text(size = 20),
        strip.text.x = element_text(hjust = 0.5))

# inspect plot
plotExperiment_Aggregate_c

# save plot
ggsave("plots/mainTreatmentEffect_Experiment_controls.png", plotExperiment_Aggregate_c, device = "jpeg", width = 4200 , height = 2000, units = "px")



########################################
# S3: Country-level effects with controls

# Experiment 1: Military intervention with controls
countryModel_ex1_c <- dat %>%
  group_by(QCOUNTRY) %>%
  group_map(~ broom::tidy(lm(costExperiment1_CrisisManagement ~
                               costExperiment1_Treat1 +
                               costExperiment1_Treat2 +
                               attitudes_EU +
                               attachmentCountry +
                               attachmentEurope +
                               compromise +
                               militarism +
                               threat +
                               quiz +
                               governmentVote,
                             data = .x)))

# add country names
names(countryModel_ex1_c) <- unique(dat$QCOUNTRY)

# transform and clean data
countryModel_ex1Data_c <- bind_rows(countryModel_ex1_c, .id = "QCOUNTRY") %>%
  filter(term == "costExperiment1_Treat1SoldiersRisk" |
           term == "costExperiment1_Treat2CountryCost" |
           term == "costExperiment1_Treat2SharedCost") %>%
  mutate(term = as.factor(term)) %>%
  dplyr::rename(model = QCOUNTRY) %>%
  mutate(model = as.factor(model)) %>%
  dplyr::rename(model1 = term,
                term = model) %>%
  dplyr::rename(model = model1) %>%
  mutate(model = fct_recode(model,
                            `Casualties` = "costExperiment1_Treat1SoldiersRisk",
                            `National Budget` = "costExperiment1_Treat2CountryCost",
                            `Pooled Budget` = "costExperiment1_Treat2SharedCost"),
         ex = "Military Intervention")

# plot data
plot_countryModel_ex1_c <- dwplot(countryModel_ex1Data_c,
                                  vline = geom_vline(xintercept = 0, lty = 2, color = "darkgrey", size = 1),
                                  dot_args = list(size = 2),
                                  whisker_args = list(size = 0.65)) +
  facet_wrap(vars(factor(model, levels = c("Casualties", "National Budget", "Pooled Budget")))) +
  theme_bw() +
  labs(y = "", x = "") +
  scale_color_grey(start = .1, end = .1) +
  lims(x = c(-0.15, 0.15)) +
  theme(legend.position="none",
        text = element_text(size = 20))

# Experiment 2: Aircraft procurement with controls
countryModel_ex2_c <- dat %>%
  group_by(QCOUNTRY) %>%
  group_map(~ broom::tidy(lm(costExperiment2_Aircraft ~
                               costExperiment2_Treat1 +
                               costExperiment2_Treat2 +
                               attitudes_EU +
                               attachmentCountry +
                               attachmentEurope +
                               compromise +
                               militarism +
                               threat +
                               quiz +
                               governmentVote,
                             data = .x)))

# add country names
names(countryModel_ex2_c) <- unique(dat$QCOUNTRY)

# transform and clean data
countryModel_ex2Data_c <- bind_rows(countryModel_ex2_c, .id = "QCOUNTRY") %>%
  filter(term == "costExperiment2_Treat1JobCuts" |
           term == "costExperiment2_Treat2CountryCost" |
           term == "costExperiment2_Treat2SharedCost") %>%
  mutate(term = as.factor(term)) %>%
  dplyr::rename(model = QCOUNTRY) %>%
  mutate(model = as.factor(model)) %>%
  dplyr::rename(model1 = term,
                term = model) %>%
  dplyr::rename(model = model1) %>%
  mutate(model = fct_recode(model,
                            `Job Cuts` = "costExperiment2_Treat1JobCuts",
                            `National Budget` = "costExperiment2_Treat2CountryCost",
                            `Pooled Budget` = "costExperiment2_Treat2SharedCost"),
         ex = "Defence Procurement")

# plot data
plot_countryModel_ex2_c <- dwplot(countryModel_ex2Data_c,
                                  vline = geom_vline(xintercept = 0, lty = 2, color = "darkgrey", size = 0.75),
                                  dot_args = list(size = 2),
                                  whisker_args = list(size = 0.65)) +
  facet_wrap(vars(factor(model, levels = c("Job Cuts", "National Budget", "Pooled Budget")))) +
  theme_bw() +
  labs(y = "Country", x = "Estimates") +
  scale_color_grey(start = .1, end = .1) +
  lims(x = c(-0.25, 0.25)) +
  theme(legend.position="none",
        text = element_text(size = 20))

# bind data from both experiments (with controls)
all_experiments_country <- rbind(countryModel_ex1Data_c,
                                 countryModel_ex2Data_c
)

# define some plot aesthetics
font_strips <- strip_nested(
  # Horizontal strips
  size = "constant",
  text_x = elem_list_text(face = c("bold", "italic"),
                          size = c(24,20)),
  by_layer_x = TRUE,
  bleed = TRUE,
  clip = "off"
)

# relevel model variable
all_experiments_country$model <- forcats::fct_relevel(all_experiments_country$model,
                                                      "Casualties",
                                                      "Job Cuts",
                                                      "National Budget",
                                                      "Pooled Budget")

# plot effects from both experiments with controls
plot_countryModel_ex <- ggplot(all_experiments_country,
                               aes(estimate, fct_rev(term)
                               )) +
  geom_vline(xintercept = 0, lty = 2, color = "grey", size = 0.75) +
  geom_point(size = 3, position = position_dodge(.2)) +
  geom_linerange(aes(xmin = estimate -1.96*std.error,
                     xmax = estimate + 1.96*std.error),
                 size = 1.5,
                 position = position_dodge(.2),
                 alpha = 0.6) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 14,
                 axis_text_size = 18,
                 strip_text_size = 36) +
  labs(y = "", x = "") +
  facet_nested(~ factor(ex, levels = c("Military Intervention", "Defence Procurement"))  +
                 factor(model), strip = font_strips) +
  theme(legend.position = "none",
        legend.title= element_blank()) +
  lims(x = c(-0.25, 0.15)) +
  #coord_flip() +
  theme(strip.text = element_text(hjust = .5))

# manually build plot
gt <- ggplot_gtable(ggplot_build(plot_countryModel_ex))

# define spacing between facets
gt$widths[7] = 0.325*gt$widths[7]
gt$widths[11] = 0.25*gt$widths[11]
gt$widths[15] = 1*gt$widths[15]
gt$widths[19] = 0.25*gt$widths[19]
gt$widths[23] = 0.25*gt$widths[23]

# draw plot
grid.draw(gt)

# save plot
ggsave("plots/mainTreatmentEffect_Countries_c.png", gt, device = "jpeg", width = 4600 , height = 3000, units = "px")




##############################
# S4: Group size of opposition

# Experiment 1: Baseline Opposition in Control Group by Country
opposeExp1_baselineCOUNTRY <- dat %>%
  dplyr::select(costExperiment1_CrisisManagement, costExperiment1_Treat1, costExperiment1_Treat2, QCOUNTRY) %>%
  filter(costExperiment1_Treat1 == "Control" & costExperiment1_Treat2 == "Control") %>%
  mutate(costExperiment1_CrisisManagement_supp = if_else(costExperiment1_CrisisManagement < 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  summarise(meanOppose = mean(costExperiment1_CrisisManagement_supp, na.rm = T))

# Opposition: Experiment 1, Treatment SoldiersRisk by Country
opposeExp1_Treat1_COUNTRY <- dat %>%
  dplyr::select(costExperiment1_CrisisManagement, costExperiment1_Treat1, costExperiment1_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment1_Treat1 == "SoldiersRisk" & costExperiment1_Treat2 == "Control") %>%
  mutate(costExperiment1_CrisisManagement_supp = if_else(costExperiment1_CrisisManagement < 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanOpposeTreat = mean(costExperiment1_CrisisManagement_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso, meanOpposeTreat) %>%
  unique()

# Opposition: Experiment 1, Treatment CountryCost by Country
opposeExp1_Treat21_COUNTRY <- dat %>%
  dplyr::select(costExperiment1_CrisisManagement, costExperiment1_Treat1, costExperiment1_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment1_Treat1 == "Control" & costExperiment1_Treat2 == "CountryCost") %>%
  mutate(costExperiment1_CrisisManagement_supp = if_else(costExperiment1_CrisisManagement < 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanOpposeTreat = mean(costExperiment1_CrisisManagement_supp, na.rm = T))  %>%
  dplyr::select(QCOUNTRY, iso, meanOpposeTreat) %>%
  unique()

# Opposition: Treatment Experiment 1, SharedCost by Country
opposeExp1_Treat22_COUNTRY <- dat %>%
  dplyr::select(costExperiment1_CrisisManagement, costExperiment1_Treat1, costExperiment1_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment1_Treat1 == "Control" & costExperiment1_Treat2 == "SharedCost") %>%
  mutate(costExperiment1_CrisisManagement_supp = if_else(costExperiment1_CrisisManagement < 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanOpposeTreat = mean(costExperiment1_CrisisManagement_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso, meanOpposeTreat) %>%
  unique()



# Experiment 2: Baseline Opposition in Control Group by Country
opposeExp2_baselineCOUNTRY <- dat %>%
  dplyr::select(costExperiment2_Aircraft, costExperiment2_Treat1, costExperiment2_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment2_Treat1 == "Control" & costExperiment2_Treat2 == "Control") %>%
  mutate(costExperiment2_Aircraft_supp = if_else(costExperiment2_Aircraft < 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanOppose = mean(costExperiment2_Aircraft_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso, meanOppose) %>%
  unique()

# Opposition in Treatment Group, Experiment 2, Treatment JobCuts by Country
opposeExp2_Treat1_COUNTRY <- dat %>%
  dplyr::select(costExperiment2_Aircraft, costExperiment2_Treat1, costExperiment2_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment2_Treat1 == "JobCuts" & costExperiment2_Treat2 == "Control") %>%
  mutate(costExperiment2_Aircraft_supp = if_else(costExperiment2_Aircraft < 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanOpposeTreat = mean(costExperiment2_Aircraft_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso, meanOpposeTreat) %>%
  unique()

# Opposition in Treatment Group: Experiment 2, Treatment CountryCost by Country
opposeExp2_Treat21_COUNTRY <- dat %>%
  dplyr::select(costExperiment2_Aircraft, costExperiment2_Treat1, costExperiment2_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment2_Treat1 == "Control" & costExperiment2_Treat2 == "CountryCost") %>%
  mutate(costExperiment2_Aircraft_supp = if_else(costExperiment2_Aircraft < 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanOpposeTreat = mean(costExperiment2_Aircraft_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso, meanOpposeTreat) %>%
  unique()

# Opposition in Treatment Group: Experiment 2, Treatment SharedCost by Country
opposeExp2_Treat22_COUNTRY <- dat %>%
  dplyr::select(costExperiment2_Aircraft, costExperiment2_Treat1, costExperiment2_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment2_Treat1 == "Control" & costExperiment2_Treat2 == "SharedCost") %>%
  mutate(costExperiment2_Aircraft_supp = if_else(costExperiment2_Aircraft < 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanOpposeTreat = mean(costExperiment2_Aircraft_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso, meanOpposeTreat) %>%
  unique()



##############################
# S4: Group size of neither/nor

# Experiment 1: Baseline Neither Nor in Control Group by Country
indiffExp1_baselineCOUNTRY <- dat %>%
  dplyr::select(costExperiment1_CrisisManagement, costExperiment1_Treat1, costExperiment1_Treat2, QCOUNTRY) %>%
  filter(costExperiment1_Treat1 == "Control" & costExperiment1_Treat2 == "Control") %>%
  mutate(costExperiment1_CrisisManagement_supp = if_else(costExperiment1_CrisisManagement == 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  summarise(meanIndiff = mean(costExperiment1_CrisisManagement_supp, na.rm = T))


# Neither Nor: Experiment 1, Treatment SoldiersRisk by Country
indiffExp1_Treat1_COUNTRY <- dat %>%
  dplyr::select(costExperiment1_CrisisManagement, costExperiment1_Treat1, costExperiment1_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment1_Treat1 == "SoldiersRisk" & costExperiment1_Treat2 == "Control") %>%
  mutate(costExperiment1_CrisisManagement_supp = if_else(costExperiment1_CrisisManagement == 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanIndiffTreat = mean(costExperiment1_CrisisManagement_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso, meanIndiffTreat) %>%
  unique()

# Neither Nor: Experiment 1, Treatment CountryCost by Country
indiffExp1_Treat21_COUNTRY <- dat %>%
  dplyr::select(costExperiment1_CrisisManagement, costExperiment1_Treat1, costExperiment1_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment1_Treat1 == "Control" & costExperiment1_Treat2 == "CountryCost") %>%
  mutate(costExperiment1_CrisisManagement_supp = if_else(costExperiment1_CrisisManagement == 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanIndiffTreat = mean(costExperiment1_CrisisManagement_supp, na.rm = T))  %>%
  dplyr::select(QCOUNTRY, iso, meanIndiffTreat) %>%
  unique()

# Neither Nor: Treatment Experiment 1, SharedCost by Country
indiffExp1_Treat22_COUNTRY <- dat %>%
  dplyr::select(costExperiment1_CrisisManagement, costExperiment1_Treat1, costExperiment1_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment1_Treat1 == "Control" & costExperiment1_Treat2 == "SharedCost") %>%
  mutate(costExperiment1_CrisisManagement_supp = if_else(costExperiment1_CrisisManagement == 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanIndiffTreat = mean(costExperiment1_CrisisManagement_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso, meanIndiffTreat) %>%
  unique()


# Experiment 2: Baseline Neither Nor in Control Group by Country
indiffExp2_baselineCOUNTRY <- dat %>%
  dplyr::select(costExperiment2_Aircraft, costExperiment2_Treat1, costExperiment2_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment2_Treat1 == "Control" & costExperiment2_Treat2 == "Control") %>%
  mutate(costExperiment2_Aircraft_supp = if_else(costExperiment2_Aircraft == 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanIndiff = mean(costExperiment2_Aircraft_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso, meanIndiff) %>%
  unique()

# Neither Nor in Treatment Group: Experiment 2, Treatment JobCuts by Country
indiffExp2_Treat1_COUNTRY <- dat %>%
  dplyr::select(costExperiment2_Aircraft, costExperiment2_Treat1, costExperiment2_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment2_Treat1 == "JobCuts" & costExperiment2_Treat2 == "Control") %>%
  mutate(costExperiment2_Aircraft_supp = if_else(costExperiment2_Aircraft == 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanIndiffTreat = mean(costExperiment2_Aircraft_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso, meanIndiffTreat) %>%
  unique()

# Neither Nor in Treatment Group: Experiment 2, Treatment CountryCost by Country
indiffExp2_Treat21_COUNTRY <- dat %>%
  dplyr::select(costExperiment2_Aircraft, costExperiment2_Treat1, costExperiment2_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment2_Treat1 == "Control" & costExperiment2_Treat2 == "CountryCost") %>%
  mutate(costExperiment2_Aircraft_supp = if_else(costExperiment2_Aircraft == 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanIndiffTreat = mean(costExperiment2_Aircraft_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso, meanIndiffTreat) %>%
  unique()

# Neither Nor in Treatment Group: Experiment 2, Treatment SharedCost by Country
indiffExp2_Treat22_COUNTRY <- dat %>%
  dplyr::select(costExperiment2_Aircraft, costExperiment2_Treat1, costExperiment2_Treat2, QCOUNTRY, iso) %>%
  filter(costExperiment2_Treat1 == "Control" & costExperiment2_Treat2 == "SharedCost") %>%
  mutate(costExperiment2_Aircraft_supp = if_else(costExperiment2_Aircraft == 0.5, 1,0)) %>%
  group_by(QCOUNTRY) %>%
  mutate(meanIndiffTreat = mean(costExperiment2_Aircraft_supp, na.rm = T)) %>%
  dplyr::select(QCOUNTRY, iso, meanIndiffTreat) %>%
  unique()



# Information on Experiment
opposeExp1_baselineCOUNTRY$experiment <- "Opposition towards Military Intervention"
opposeExp2_baselineCOUNTRY$experiment <- "Opposition towards Defence Procurement"

indiffExp1_baselineCOUNTRY$experiment <- "Indifference towards Military Intervention"
indiffExp2_baselineCOUNTRY$experiment <- "Indifference towards Defence Procurement"


## Country Comparisons: Opposition in Control Group vs. Opposition in Treatment Group
# Oppose: Experiment 1
opposeExp1_Treat1_comp <- left_join(opposeExp1_baselineCOUNTRY, opposeExp1_Treat1_COUNTRY) # Control vs. Casualties
opposeExp1_Treat21_comp <- left_join(opposeExp1_baselineCOUNTRY, opposeExp1_Treat21_COUNTRY) # Control vs. National Budget
opposeExp1_Treat22_comp <- left_join(opposeExp1_baselineCOUNTRY, opposeExp1_Treat22_COUNTRY) # Control vs. Pooled Budget

# Oppose: Experiment 2
opposeExp2_Treat1_comp <- left_join(opposeExp2_baselineCOUNTRY, opposeExp2_Treat1_COUNTRY) # Control vs. Job Cuts
opposeExp2_Treat21_comp <- left_join(opposeExp2_baselineCOUNTRY, opposeExp2_Treat21_COUNTRY) # Control vs. National Budget
opposeExp2_Treat22_comp <- left_join(opposeExp2_baselineCOUNTRY, opposeExp2_Treat22_COUNTRY) # Control vs. Pooled Budget


## Country Comparisons: Neither Nor in Control Group vs. Neither Nor in Treatment Group
# Neither Nor: Experiment 1
indiffExp1_Treat1_comp <- left_join(indiffExp1_baselineCOUNTRY, indiffExp1_Treat1_COUNTRY) # Control vs. Casualties
indiffExp1_Treat21_comp <- left_join(indiffExp1_baselineCOUNTRY, indiffExp1_Treat21_COUNTRY) # Control vs. National Budget
indiffExp1_Treat22_comp <- left_join(indiffExp1_baselineCOUNTRY, indiffExp1_Treat22_COUNTRY) # Control vs. Pooled Budget

# Neither Nor: Experiment 2
indiffExp2_Treat1_comp <- left_join(indiffExp2_baselineCOUNTRY, indiffExp2_Treat1_COUNTRY) # Control vs. Job Cuts
indiffExp2_Treat21_comp <- left_join(indiffExp2_baselineCOUNTRY, indiffExp2_Treat21_COUNTRY) # Control vs. National Budget
indiffExp2_Treat22_comp <- left_join(indiffExp2_baselineCOUNTRY, indiffExp2_Treat22_COUNTRY) # Control vs. Pooled Budget



### Opposition Plots
# Opposition: Experiment 1, Treatment 1
# Red Colour for countries with changing majorities
opposeExp1_Treat1_comp$red <- ifelse(opposeExp1_Treat1_comp$meanOppose > 0.5 & opposeExp1_Treat1_comp$meanOpposeTreat < 0.5 |
                                       opposeExp1_Treat1_comp$meanOppose < 0.5 & opposeExp1_Treat1_comp$meanOpposeTreat > 0.5,
                                     TRUE, FALSE)
# plot results
opp_plotDescrEx1_Treat1 <- opposeExp1_Treat1_comp %>%
  ggplot(., aes(x = meanOpposeTreat, y = meanOppose, label = iso, country = iso, color = red)) +
  labs(x = "Opposition in Treatment Group", y = "Opposition in Control Group",
       title = "Military Intervention: Casualties") +
  lims(y = c(0.1,0.8), x = c(0.1,0.8)) +
  geom_abline(intercept = 0, slope = 1, lty = "dotted", color = "darkgrey", size = 0.5) +
  geom_vline(xintercept = 0.5, lty = 1, color = "darkgrey", size = 0.5) +
  geom_hline(yintercept = 0.5, lty = 1, color = "darkgrey", size = 0.5) +
  theme_ipsum_rc(plot_title_size = 20,
                 base_size = 18,
                 axis_title_size = 18) +
  geom_text(size = 5, family = "Roboto Condensed") +
  scale_color_manual(values = c("black", "red")) +
  theme(legend.position = "none")


# Oppose: Experiment 1, Treatment 2_1
# Red Colour for countries with changing majorities
opposeExp1_Treat21_comp$red <- ifelse(opposeExp1_Treat21_comp$meanOppose > 0.5 & opposeExp1_Treat21_comp$meanOpposeTreat < 0.5 |
                                        opposeExp1_Treat21_comp$meanOppose < 0.5 & opposeExp1_Treat21_comp$meanOpposeTreat > 0.5,
                                      TRUE, FALSE)
# plot results
opp_plotDescrEx1_Treat21 <- opposeExp1_Treat21_comp %>%
  ggplot(., aes(x = meanOpposeTreat, y = meanOppose, label = iso, country = iso, color = red)) +
  labs(x = "Opposition in Treatment Group", y = "Opposition in Control Group",
       title = "Military Intervention: National Budget") +
  lims(y = c(0.1,0.8), x = c(0.1,0.8)) +
  geom_abline(intercept = 0, slope = 1, lty = "dotted", color = "darkgrey", size = 1) +
  geom_vline(xintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  geom_hline(yintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  theme_ipsum_rc(plot_title_size = 20,
                 base_size = 18,
                 axis_title_size = 18) +
  geom_text(size = 5, family = "Roboto Condensed") +
  scale_color_manual(values = c("black", "red")) +
  theme(legend.position = "none")


# Oppose: Experiment 1, Treatment 2_2
# Red Colour for countries with changing majorities
opposeExp1_Treat22_comp$red <- ifelse(opposeExp1_Treat22_comp$meanOppose > 0.5 & opposeExp1_Treat22_comp$meanOpposeTreat < 0.5 |
                                        opposeExp1_Treat22_comp$meanOppose < 0.5 & opposeExp1_Treat22_comp$meanOpposeTreat > 0.5,
                                      TRUE, FALSE)

# plot results
opp_plotDescrEx1_Treat22 <- opposeExp1_Treat22_comp %>%
  ggplot(., aes(x = meanOpposeTreat, y = meanOppose, label = iso, country = iso, color = red)) +
  labs(x = "Opposition in Treatment Group", y = "Opposition in Control Group",
       title = "Military Intervention: Pooled Budget") +
  lims(y = c(0.05,0.8), x = c(0.05,0.8)) +
  geom_abline(intercept = 0, slope = 1, lty = "dotted", color = "darkgrey", size = 1) +
  geom_vline(xintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  geom_hline(yintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  theme_ipsum_rc(plot_title_size = 20,
                 base_size = 18,
                 axis_title_size = 18) +
  geom_text(size = 5, family = "Roboto Condensed") +
  scale_color_manual(values = c("black", "red")) +
  theme(legend.position = "none")


# Oppose: Experiment 2, Treatment 1
# Red Colour for countries with changing majorities
opposeExp2_Treat1_comp$red <- ifelse(opposeExp2_Treat1_comp$meanOppose > 0.5 & opposeExp2_Treat1_comp$meanOpposeTreat < 0.5 |
                                       opposeExp2_Treat1_comp$meanOppose < 0.5 & opposeExp2_Treat1_comp$meanOpposeTreat > 0.5,
                                     TRUE, FALSE)

# plot results
opp_plotDescrEx2_Treat1 <- opposeExp2_Treat1_comp %>%
  ggplot(., aes(x = meanOpposeTreat, y = meanOppose, label = iso, country = iso, color = red)) +
  labs(x = "Opposition in Treatment Group", y = "Opposition in Control Group",
       title = "Defence Procurement: Job Cuts") +
  lims(y = c(0.05,0.8), x = c(0.05,0.8)) +
  geom_abline(intercept = 0, slope = 1, lty = "dotted", color = "darkgrey", size = 1) +
  geom_vline(xintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  geom_hline(yintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  theme_ipsum_rc(plot_title_size = 20,
                 base_size = 18,
                 axis_title_size = 18) +
  geom_text(size = 5, family = "Roboto Condensed") +
  scale_color_manual(values = c("black", "red")) +
  theme(legend.position = "none")

# Oppose: Experiment 2, Treatment 2_1
# Red Colour for countries with changing majorities
opposeExp2_Treat21_comp$red <- ifelse(opposeExp2_Treat21_comp$meanOppose > 0.5 & opposeExp2_Treat21_comp$meanOpposeTreat < 0.5 |
                                        opposeExp2_Treat21_comp$meanOppose < 0.5 & opposeExp2_Treat21_comp$meanOpposeTreat > 0.5,
                                      TRUE, FALSE)

# plot results
opp_plotDescrEx2_Treat21 <- opposeExp2_Treat21_comp %>%
  ggplot(., aes(x = meanOpposeTreat, y = meanOppose, label = iso, country = iso, color = red)) +
  labs(x = "Opposition in Treatment Group", y = "Opposition in Control Group",
       title = "Defence Procurement: National Budget") +
  lims(y = c(0.05,0.8), x = c(0.05,0.8)) +
  geom_abline(intercept = 0, slope = 1, lty = "dotted", color = "darkgrey", size = 1) +
  geom_vline(xintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  geom_hline(yintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  theme_ipsum_rc(plot_title_size = 20,
                 base_size = 18,
                 axis_title_size = 18) +
  geom_text(size = 5, family = "Roboto Condensed") +
  scale_color_manual(values = c("black", "red")) +
  theme(legend.position = "none")

# Oppose: Experiment 2, Treatment 2_2
# Red Colour for countries with changing majorities
opposeExp2_Treat22_comp$red <- ifelse(opposeExp2_Treat22_comp$meanOppose > 0.5 & opposeExp2_Treat22_comp$meanOpposeTreat < 0.5 |
                                        opposeExp2_Treat22_comp$meanOppose < 0.5 & opposeExp2_Treat22_comp$meanOpposeTreat > 0.5,
                                      TRUE, FALSE)

# plot results
opp_plotDescrEx2_Treat22 <- opposeExp2_Treat22_comp %>%
  ggplot(., aes(x = meanOpposeTreat, y = meanOppose, label = iso, country = iso, color = red)) +
  labs(x = "Opposition in Treatment Group", y = "Opposition in Control Group",
       title = "Defence Procurement: Pooled Budget") +
  lims(y = c(0.05,0.8), x = c(0.05,0.8)) +
  geom_abline(intercept = 0, slope = 1, lty = "dotted", color = "darkgrey", size = 1) +
  geom_vline(xintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  geom_hline(yintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  theme_ipsum_rc(plot_title_size = 20,
                 base_size = 18,
                 axis_title_size = 18) +
  geom_text(size = 5, family = "Roboto Condensed") +
  scale_color_manual(values = c("black", "red")) +
  theme(legend.position = "none")

## arrange plots
all_plots_opp <- grid.arrange(opp_plotDescrEx1_Treat1, opp_plotDescrEx1_Treat21, opp_plotDescrEx1_Treat22,
                              opp_plotDescrEx2_Treat1, opp_plotDescrEx2_Treat21, opp_plotDescrEx2_Treat22, nrow = 2)


ggsave("plots/OppositionControlTreatmentComparison.png", all_plots_opp, device = "jpeg", width = 4800 , height = 3200, units = "px")



### Indifference Plots
# Opposition: Experiment 1, Treatment 1
# Red Colour for countries with changing majorities
indiffExp1_Treat1_comp$red <- ifelse(indiffExp1_Treat1_comp$meanIndiff > 0.5 & indiffExp1_Treat1_comp$meanIndiffTreat < 0.5 |
                                       indiffExp1_Treat1_comp$meanIndiff < 0.5 & indiffExp1_Treat1_comp$meanIndiffTreat > 0.5,
                                     TRUE, FALSE)

# plot results
ind_plotDescrEx1_Treat1 <- indiffExp1_Treat1_comp %>%
  ggplot(., aes(x = meanIndiffTreat, y = meanIndiff, label = iso, country = iso, color = red)) +
  labs(x = "Indifference in Treatment Group", y = "Indifference in Control Group",
       title = "Military Intervention: Casualties") +
  lims(y = c(0.15,0.8), x = c(0.15,0.8)) +
  geom_abline(intercept = 0, slope = 1, lty = "dotted", color = "darkgrey", size = 0.5) +
  geom_vline(xintercept = 0.5, lty = 1, color = "darkgrey", size = 0.5) +
  geom_hline(yintercept = 0.5, lty = 1, color = "darkgrey", size = 0.5) +
  theme_ipsum_rc(plot_title_size = 20,
                 base_size = 18,
                 axis_title_size = 18) +
  geom_text(size = 5, family = "Roboto Condensed") +
  scale_color_manual(values = c("black", "red")) +
  theme(legend.position = "none")


# Indiff: Experiment 1, Treatment 2_1
# Red Colour for countries with changing majorities
indiffExp1_Treat21_comp$red <- ifelse(indiffExp1_Treat21_comp$meanIndiff > 0.5 & indiffExp1_Treat21_comp$meanIndiffTreat < 0.5 |
                                        indiffExp1_Treat21_comp$meanIndiff < 0.5 & indiffExp1_Treat21_comp$meanIndiffTreat > 0.5,
                                      TRUE, FALSE)

# plot results
ind_plotDescrEx1_Treat21 <- indiffExp1_Treat21_comp %>%
  ggplot(., aes(x = meanIndiffTreat, y = meanIndiff, label = iso, country = iso, color = red)) +
  labs(x = "Indifference in Treatment Group", y = "Indifference in Control Group",
       title = "Military Intervention: National Budget") +
  lims(y = c(0.15,0.8), x = c(0.15,0.8)) +
  geom_abline(intercept = 0, slope = 1, lty = "dotted", color = "darkgrey", size = 1) +
  geom_vline(xintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  geom_hline(yintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  theme_ipsum_rc(plot_title_size = 20,
                 base_size = 18,
                 axis_title_size = 18) +
  geom_text(size = 5, family = "Roboto Condensed") +
  scale_color_manual(values = c("black", "red")) +
  theme(legend.position = "none")


# Indiff: Experiment 1, Treatment 2_2
# Red Colour for countries with changing majorities
indiffExp1_Treat22_comp$red <- ifelse(indiffExp1_Treat22_comp$meanIndiff > 0.5 & indiffExp1_Treat22_comp$meanIndiffTreat < 0.5 |
                                        indiffExp1_Treat22_comp$meanIndiff < 0.5 & indiffExp1_Treat22_comp$meanIndiffTreat > 0.5,
                                      TRUE, FALSE)

# plot results
ind_plotDescrEx1_Treat22 <- indiffExp1_Treat22_comp %>%
  ggplot(., aes(x = meanIndiffTreat, y = meanIndiff, label = iso, country = iso, color = red)) +
  labs(x = "Indifference in Treatment Group", y = "Indifference in Control Group",
       title = "Military Intervention: Pooled Budget") +
  lims(y = c(0.15,0.8), x = c(0.15,0.8)) +
  geom_abline(intercept = 0, slope = 1, lty = "dotted", color = "darkgrey", size = 1) +
  geom_vline(xintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  geom_hline(yintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  theme_ipsum_rc(plot_title_size = 20,
                 base_size = 18,
                 axis_title_size = 18) +
  geom_text(size = 5, family = "Roboto Condensed") +
  scale_color_manual(values = c("black", "red")) +
  theme(legend.position = "none")


# Indiff: Experiment 2, Treatment 1
# Red Colour for countries with changing majorities
indiffExp2_Treat1_comp$red <- ifelse(indiffExp2_Treat1_comp$meanIndiff > 0.5 & indiffExp2_Treat1_comp$meanIndiffTreat < 0.5 |
                                       indiffExp2_Treat1_comp$meanIndiff < 0.5 & indiffExp2_Treat1_comp$meanIndiffTreat > 0.5,
                                     TRUE, FALSE)

# plot results
ind_plotDescrEx2_Treat1 <- indiffExp2_Treat1_comp %>%
  ggplot(., aes(x = meanIndiffTreat, y = meanIndiff, label = iso, country = iso, color = red)) +
  labs(x = "Indifference in Treatment Group", y = "Indifference in Control Group",
       title = "Defence Procurement: Job Cuts") +
  lims(y = c(0.15,0.8), x = c(0.15,0.8)) +
  geom_abline(intercept = 0, slope = 1, lty = "dotted", color = "darkgrey", size = 1) +
  geom_vline(xintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  geom_hline(yintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  theme_ipsum_rc(plot_title_size = 20,
                 base_size = 18,
                 axis_title_size = 18) +
  geom_text(size = 5, family = "Roboto Condensed") +
  scale_color_manual(values = c("black", "red")) +
  theme(legend.position = "none")

# Indiff: Experiment 2, Treatment 2_1
# Red Colour for countries with changing majorities
indiffExp2_Treat21_comp$red <- ifelse(indiffExp2_Treat21_comp$meanIndiff > 0.5 & indiffExp2_Treat21_comp$meanIndiffTreat < 0.5 |
                                        indiffExp2_Treat21_comp$meanIndiff < 0.5 & indiffExp2_Treat21_comp$meanIndiffTreat > 0.5,
                                      TRUE, FALSE)

# plot results
ind_plotDescrEx2_Treat21 <- indiffExp2_Treat21_comp %>%
  ggplot(., aes(x = meanIndiffTreat, y = meanIndiff, label = iso, country = iso, color = red)) +
  labs(x = "Indifference in Treatment Group", y = "Indifference in Control Group",
       title = "Defence Procurement: National Budget") +
  lims(y = c(0.15,0.8), x = c(0.15,0.8)) +
  geom_abline(intercept = 0, slope = 1, lty = "dotted", color = "darkgrey", size = 1) +
  geom_vline(xintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  geom_hline(yintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  theme_ipsum_rc(plot_title_size = 20,
                 base_size = 18,
                 axis_title_size = 18) +
  geom_text(size = 5, family = "Roboto Condensed") +
  scale_color_manual(values = c("black", "red")) +
  theme(legend.position = "none")

# Indiff: Experiment 2, Treatment 2_2
# Red Colour for countries with changing majorities
indiffExp2_Treat22_comp$red <- ifelse(indiffExp2_Treat22_comp$meanIndiff > 0.5 & indiffExp2_Treat22_comp$meanIndiffTreat < 0.5 |
                                        indiffExp2_Treat22_comp$meanIndiff < 0.5 & indiffExp2_Treat22_comp$meanIndiffTreat > 0.5,
                                      TRUE, FALSE)

# plot results
ind_plotDescrEx2_Treat22 <- indiffExp2_Treat22_comp %>%
  ggplot(., aes(x = meanIndiffTreat, y = meanIndiff, label = iso, country = iso, color = red)) +
  labs(x = "Indifference in Treatment Group", y = "Indifference in Control Group",
       title = "Defence Procurement: Pooled Budget") +
  lims(y = c(0.15,0.8), x = c(0.15,0.8)) +
  geom_abline(intercept = 0, slope = 1, lty = "dotted", color = "darkgrey", size = 1) +
  geom_vline(xintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  geom_hline(yintercept = 0.5, lty = 1, color = "darkgrey", size = 1) +
  theme_ipsum_rc(plot_title_size = 20,
                 base_size = 18,
                 axis_title_size = 18) +
  geom_text(size = 5, family = "Roboto Condensed") +
  scale_color_manual(values = c("black", "red")) +
  theme(legend.position = "none")

## arrange plots
all_plots_ind <- grid.arrange(ind_plotDescrEx1_Treat1, ind_plotDescrEx1_Treat21, ind_plotDescrEx1_Treat22,
                              ind_plotDescrEx2_Treat1, ind_plotDescrEx2_Treat21, ind_plotDescrEx2_Treat22, nrow = 2)

# save plots 
ggsave("plots/IndifferenceControlTreatmentComparison.png", all_plots_ind, device = "jpeg", width = 4800 , height = 3200, units = "px")


############################################
# S5: Additional individual-level moderators

## Cooperative Internationalism, heterogeneous treatment effect
# Experiment 1: Interaction Model
CI_ex1 <- dat %>%
  mutate(costExperiment1_Treat1 = fct_recode(costExperiment1_Treat1, `Casualties` = "SoldiersRisk"),
         costExperiment1_Treat2 = fct_recode(costExperiment1_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost")) %>%
  lm(costExperiment1_CrisisManagement ~  costExperiment1_Treat1 * compromise + costExperiment1_Treat2 * compromise  + QCOUNTRY, data = .)

# Experiment 2: Interaction Model
CI_ex2 <- dat %>%
  mutate(costExperiment2_Treat1 = fct_recode(costExperiment2_Treat1, `Job Cuts` = "JobCuts"),
         costExperiment2_Treat2 = fct_recode(costExperiment2_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost")) %>%
  lm(costExperiment2_Aircraft ~ costExperiment2_Treat1 * compromise + costExperiment2_Treat2 * compromise + QCOUNTRY, data = .)

# Military Intervention: Casualties x Cooperative Internationalism
plot_CI_Ex1_1 <- plot_model(CI_ex1, type = "pred", terms = c("compromise", "costExperiment1_Treat1"), colors = c("#87a96b", "#800080")) +
  labs(y = "Predicted Value", x = "Cooperative Internationalism", title = "Support for Military Intervention", color = "Non-Budgetary Costs") +
  lims(y = c(0.1,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm")) #+ coord_fixed(ratio=1)

# Military Intervention: Budgetary Costs x Cooperative Internationalism
plot_CI_Ex1_2 <- plot_model(CI_ex1, type = "pred", terms = c("compromise", "costExperiment1_Treat2"), colors = c("#87a96b", "#ffb653", "#6b87a9")) +
  labs(y = "Predicted Value", x = "Cooperative Internationalism", title = "Support for Military Intervention", color = "Budgetary Costs") +
  lims(y = c(0.1,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm")) #+ coord_fixed(ratio=1)


# Defence Procurement: Job Cuts x Cooperative Internationalism
plot_CI_Ex2_1 <- plot_model(CI_ex2, type = "pred", terms = c("compromise", "costExperiment2_Treat1"), colors = c("#87a96b", "#800080")) +
  labs(y = "Predicted Value", x = "Cooperative Internationalism", title = "Support for Defence Procurement", color = "Non-Budgetary Costs") +
  lims(y = c(0.1,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm")) #+ coord_fixed(ratio=1)

# Defence Procurement: Budgetary Costs x Cooperative Internationalism
plot_CI_Ex2_2 <- plot_model(CI_ex2, type = "pred", terms = c("compromise", "costExperiment2_Treat2"), colors = c("#87a96b", "#ffb653", "#6b87a9")) +
  labs(y = "Predicted Value", x = "Cooperative Internationalism", title = "Support for Defence Procurement", color = "Budgetary Costs") +
  lims(y = c(0.1,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm")) #+ coord_fixed(ratio=1)



## National Attachment, heterogeneous treatment effect
# Experiment 1: Interaction Model
NatAttach_ex1 <- dat %>%
  mutate(costExperiment1_Treat1 = fct_recode(costExperiment1_Treat1, `Casualties` = "SoldiersRisk"),
         costExperiment1_Treat2 = fct_recode(costExperiment1_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost")) %>%
  lm(costExperiment1_CrisisManagement ~  costExperiment1_Treat1 * attachmentCountry + costExperiment1_Treat2 * attachmentCountry  + QCOUNTRY, data = .)

# Experiment 2: Interaction Model
NatAttach_ex2 <- dat %>%
  mutate(costExperiment2_Treat1 = fct_recode(costExperiment2_Treat1, `Job Cuts` = "JobCuts"),
         costExperiment2_Treat2 = fct_recode(costExperiment2_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost")) %>%
  lm(costExperiment2_Aircraft ~ costExperiment2_Treat1 * attachmentCountry + costExperiment2_Treat2 * attachmentCountry + QCOUNTRY, data = .)

# Military Intervention: Casualties x National Attachment
plot_NatAttach_Ex1_1 <- plot_model(NatAttach_ex1, type = "pred", terms = c("attachmentCountry", "costExperiment1_Treat1"), colors = c("#87a96b", "#800080")) +
  labs(y = "Predicted Value", x = "National Attachment", title = "Support for Military Intervention", color = "Non-Budgetary Costs") +
  lims(y = c(0.1,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm")) #+ coord_fixed(ratio=1)

# Military Intervention: Budgetary Costs x National Attachment
plot_NatAttach_Ex1_2 <- plot_model(NatAttach_ex1, type = "pred", terms = c("attachmentCountry", "costExperiment1_Treat2"), colors = c("#87a96b", "#ffb653", "#6b87a9")) +
  labs(y = "Predicted Value", x = "National Attachment", title = "Support for Military Intervention", color = "Budgetary Costs") +
  lims(y = c(0.1,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm")) #+ coord_fixed(ratio=1)

# Defence Procurement: Job Cuts x National Attachment
plot_NatAttach_Ex2_1 <- plot_model(NatAttach_ex2, type = "pred", terms = c("attachmentCountry", "costExperiment2_Treat1"), colors = c("#87a96b", "#800080")) +
  labs(y = "Predicted Value", x = "National Attachment", title = "Support for Defence Procurement", color = "Non-Budgetary Costs") +
  lims(y = c(0.1,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm")) #+ coord_fixed(ratio=1)

# Defence Procurement: Budgetary Costs x National Attachment
plot_NatAttach_Ex2_2 <- plot_model(NatAttach_ex2, type = "pred", terms = c("attachmentCountry", "costExperiment2_Treat2"), colors = c("#87a96b", "#ffb653", "#6b87a9")) +
  labs(y = "Predicted Value", x = "National Attachment", title = "Support for Defence Procurement", color = "Budgetary Costs") +
  lims(y = c(0.1,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm")) #+ coord_fixed(ratio=1)


## Attachment to Europe heterogeneous treatment effect
# Experiment 1: Interaction Model
EUAttach_ex1 <- dat %>%
  mutate(costExperiment1_Treat1 = fct_recode(costExperiment1_Treat1, `Casualties` = "SoldiersRisk"),
         costExperiment1_Treat2 = fct_recode(costExperiment1_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost")) %>%
  lm(costExperiment1_CrisisManagement ~  costExperiment1_Treat1 * attachmentEurope + costExperiment1_Treat2 * attachmentEurope  + QCOUNTRY, data = .)

# Experiment 2: Interaction Model
EUAttach_ex2 <- dat %>%
  mutate(costExperiment2_Treat1 = fct_recode(costExperiment2_Treat1, `Job Cuts` = "JobCuts"),
         costExperiment2_Treat2 = fct_recode(costExperiment2_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost")) %>%
  lm(costExperiment2_Aircraft ~ costExperiment2_Treat1 * attachmentEurope + costExperiment2_Treat2 * attachmentEurope + QCOUNTRY, data = .)

# Military Intervention: Casualties x Attachment to Europe
plot_EUAttach_Ex1_1 <- plot_model(EUAttach_ex1, type = "pred", terms = c("attachmentEurope", "costExperiment1_Treat1"), colors = c("#87a96b", "#800080")) +
  labs(y = "Predicted Value", x = "Attachment to Europe", title = "Support for Military Intervention", color = "Non-Budgetary Costs") +
  lims(y = c(0.1,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm")) #+ coord_fixed(ratio=1)

# Military Intervention: Budgetary Costs x Attachment to Europe
plot_EUAttach_Ex1_2 <- plot_model(EUAttach_ex1, type = "pred", terms = c("attachmentEurope", "costExperiment1_Treat2"), colors = c("#87a96b", "#ffb653", "#6b87a9")) +
  labs(y = "Predicted Value", x = "Attachment to Europe", title = "Support for Military Intervention", color = "Budgetary Costs") +
  lims(y = c(0.1,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm")) #+ coord_fixed(ratio=1)

# Defence Procurement: Job Cuts x Attachment to Europe
plot_EUAttach_Ex2_1 <- plot_model(EUAttach_ex2, type = "pred", terms = c("attachmentEurope", "costExperiment2_Treat1"), colors = c("#87a96b", "#800080")) +
  labs(y = "Predicted Value", x = "Attachment to Europe", title = "Support for Defence Procurement", color = "Non-Budgetary Costs") +
  lims(y = c(0.1,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm")) #+ coord_fixed(ratio=1)

# Defence Procurement: Budgetary Costs x Attachment to Europe
plot_EUAttach_Ex2_2 <- plot_model(EUAttach_ex2, type = "pred", terms = c("attachmentEurope", "costExperiment2_Treat2"), colors = c("#87a96b", "#ffb653", "#6b87a9")) +
  labs(y = "Predicted Value", x = "Attachment to Europe", title = "Support for Defence Procurement", color = "Budgetary Costs") +
  lims(y = c(0.1,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm")) #+ coord_fixed(ratio=1)


# define function to arrange plots
grid_arrange_shared_legend <- function(..., ncol = length(list(...)), nrow = 1, position = c("bottom", "right")) {
  
  plots <- list(...)
  position <- match.arg(position)
  g <- ggplotGrob(plots[[1]] + theme(legend.position = position))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  lwidth <- sum(legend$width)
  gl <- lapply(plots, function(x) x + theme(legend.position="none"))
  gl <- c(gl, ncol = ncol, nrow = nrow)
  
  combined <- switch(position,
                     "bottom" = arrangeGrob(do.call(arrangeGrob, gl),
                                            legend,
                                            ncol = 1,
                                            heights = unit.c(unit(1, "npc") - lheight, lheight)),
                     "right" = arrangeGrob(do.call(arrangeGrob, gl),
                                           legend,
                                           ncol = 2,
                                           widths = unit.c(unit(1, "npc") - lwidth, lwidth)))
  
  grid.newpage()
  grid.draw(combined)
  
  # return gtable invisibly
  invisible(combined)
  
}


## Arrange plots
# Military intervention, Non-Budgetary costs
plot_military_operation_add_1 <- grid_arrange_shared_legend(plot_CI_Ex1_1,
                                                            plot_NatAttach_Ex1_1,
                                                            plot_EUAttach_Ex1_1,ncol = 3)
# Military intervention, Budgetary costs
plot_military_operation_add_2 <- grid_arrange_shared_legend(plot_CI_Ex1_2,
                                                            plot_NatAttach_Ex1_2,
                                                            plot_EUAttach_Ex1_2,ncol = 3)

# Combine both plots
military_operation_add <- grid.arrange(plot_military_operation_add_1,
                                       whitespace,
                                       plot_military_operation_add_2,
                                       heights=c(20,1.5,20))

# export plot
ggsave("het_treatment_military_add.png", military_operation_add,
       device = "jpeg", width = 4600 , height = 3000, units = "px")

# Aircraft Procurement, Non-Budgetary costs
plot_aircraft_procurement_add_1 <- grid_arrange_shared_legend(plot_CI_Ex2_1,
                                                              plot_NatAttach_Ex2_1,
                                                              plot_EUAttach_Ex2_1,
                                                              ncol = 3)
# Aircraft Procurement, Budgetary costs
plot_aircraft_procurement_add_2 <- grid_arrange_shared_legend(plot_CI_Ex2_2,
                                                              plot_NatAttach_Ex2_2,
                                                              plot_EUAttach_Ex2_2,
                                                              ncol = 3)

# Combine both plots
aircraft_procurement_add <- grid.arrange(plot_aircraft_procurement_add_1,
                                         whitespace,
                                         plot_aircraft_procurement_add_2,
                                         heights=c(20,1.5,20))

# export plot
ggsave("het_treatment_defence_add.png", aircraft_procurement_add,
       device = "jpeg", width = 4600 , height = 3000, units = "px")
